package com.marvels.approve.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.marvels.approve.manager.dao.ICaseManagerProductTypeDao;
import com.marvels.approve.manager.dto.CaseManagerProductTypeDTO;
import com.marvels.approve.manager.service.ICaseManagerProductTypeService;
import com.marvels.common.constants.PageConstant;
import com.marvels.common.exception.MarvelsBusinessException;
@Service
public class CaseManagerProductTypeServiceImpl implements ICaseManagerProductTypeService {
	@Autowired
	private ICaseManagerProductTypeDao caseManagerProductTypeDao;
	@Override
	public List<CaseManagerProductTypeDTO> findCaseManagerProductTypes(CaseManagerProductTypeDTO ptDto)
			throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		ptDto = ptDto == null?new CaseManagerProductTypeDTO():ptDto;
		if(ptDto.getPageNum()<1){
			ptDto.setPageNum(PageConstant.CURRPAGE);
		}
		if(ptDto.getPageSize()<1){
			ptDto.setPageSize(PageConstant.PAGESIZE);
		}
		PageHelper.startPage(ptDto.getPageNum(),ptDto.getPageSize());
		return this.caseManagerProductTypeDao.select(ptDto); 
	}

	@Override
	public CaseManagerProductTypeDTO saveCaseManagerProductType(CaseManagerProductTypeDTO entity)
			throws MarvelsBusinessException { 
		if(entity.getId() == null){
			if(entity.getProductType() == null || entity.getActKey() == null){
				throw new MarvelsBusinessException("未选择产品或者流程!");
			}
			CaseManagerProductTypeDTO dto = new CaseManagerProductTypeDTO();
			dto.setProductType(entity.getProductType());
			dto.setStatus(0);
			List datas = this.caseManagerProductTypeDao.select(dto);
			if(datas!=null &&datas.size()>0){
				throw new MarvelsBusinessException("产品流程已经存在不能新增!");
			}
			//新增 
			entity.setCreatedBy(entity.obtainLoginUserName());
			entity.setUpdatedBy(entity.obtainLoginUserName());
			this.caseManagerProductTypeDao.insert(entity);
		}else{//更新
			//查询该笔产品流程
			CaseManagerProductTypeDTO dbEntity = caseManagerProductTypeDao.selectByPrimaryKey(entity.getId());
			//效验status=0的只有一笔
			CaseManagerProductTypeDTO dto = new CaseManagerProductTypeDTO();
			dto.setProductType(dbEntity.getProductType());
			List datas = this.caseManagerProductTypeDao.select(dto);
			if(datas==null || datas.size()!=1){
				throw new MarvelsBusinessException(dbEntity.getProductTypeName()+"对应流程设置有问题，请反馈开发人员!");
			}
			if(entity.getStatus() == dbEntity.getStatus()){
				throw new MarvelsBusinessException(dbEntity.getProductTypeName()+"对应流程设置状态无需更改!");
			}
			//状态更新到数据库
			entity.setUpdatedBy(entity.obtainLoginUserName());
			this.caseManagerProductTypeDao.updateByPrimaryKeySelective(entity);
		}
		return entity;
	}
 
	@Override
	public CaseManagerProductTypeDTO deleteCaseManagerProductType(CaseManagerProductTypeDTO entity)
			throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		if(entity!=null&& entity.getId()!=null){
			this.caseManagerProductTypeDao.deleteByPrimaryKey(entity.getId());
		}else{
			throw new MarvelsBusinessException("数据错误");
		}
		return entity;
	}

	@Override
	public CaseManagerProductTypeDTO findCaseManagerProductTypeById(CaseManagerProductTypeDTO entity)
			throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		if(entity!=null&& entity.getId()!=null){
			entity = this.caseManagerProductTypeDao.selectByPrimaryKey(entity.getId());
		}else{
			throw new MarvelsBusinessException("数据错误");
		}
		return entity;
	}

}
